package org.liuqm.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisConfigUtil {

    static String config = "mybatis-config.xml";
    static SqlSessionFactory sqlSessionFactory = null;

    static {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(config);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        //2、通过核心配置文件创建 sqlsessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);


    }


    public static SqlSession getSqlSession() {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        return sqlSession;
    }

    public static void closeSqlSession(SqlSession sqlSession) {
        sqlSession.close();
    }
}
